/**
 * Created by jerome.arnoult on 15/05/14.
 */

/**
 * Colors
 * - Structures :
 *      =Spawn:         #FFFFD4
 *      =Corridor:      #878787
 *      =Extractor:     #888AC2
 *      =Generator:     #E6C70A
 *      =Biodome:       #52C11E
 *      =Kitchen:       #ED5751
 *      =LifeRoom:      #DC4CD0
 *      =DefenceArea:   #5B97F9
 *      =Research:      #62FAFA
 *      =Infirmary:     #?????? // TODO : Choisir couleur pour infirmerie
 *      =DefenceAntiAir:#888DD4
 *      =Airport:       #F30707
 *      =Bunker:        #683200 // TODO : changer couleur pas assez visible
 *
 * - Entities (foncé, medium et clair) :
 *      =Thorium:   #5558A7, #7275C9, #888AC2
 *      =Energy:    #EEC922, #F4DD74
 *      =Harvest:   #7EA941, #9EC567
 *      =Food:      #EA1A1A, #F06262
 *
 * - Noids :
 *      =Supplier:  #FFF
 *      =Engineer:  #F1A829
 *      =Miner:     #F0F0F0
 *      =Harvester: #80D11D
 *      =Cooker:    #F8C1C0
 *      =defender:  #C9DDFD
 *      =researcher:#E2FEFE
 *
 * - Player :
 *      #1: #00EFFF
 *      #2: #FFF4B3
 *
 * - ERROR CONSTRUCT : #FF4949
 */



function gamerenderer(idCanvas, stage){
    this.stage = stage;
    this.intervalRender;//used only in horrible case of outdated browser
    this.stopRendering = true;
    this.optimizeRender(idCanvas, stage);
    this.toggleFPS = false;
    this.FPS;

    this.useTicker = true;

    //Graphics
    this.padding = 5;
}
//Perform optim
gamerenderer.prototype.optimizeRender = function(idCanvas, stage){
    var canvas = document.getElementById(idCanvas);
    canvas.width = (window.innerWidth) >>0;
    canvas.style.width = window.innerWidth + 'px';
    canvas.height = (window.innerHeight) >>0;
    canvas.style.height = window.innerHeight + 'px';
}
//Queueing frame
gamerenderer.prototype.queueNewFrame = function(){
    if (window.requestAnimationFrame)
        window.requestAnimationFrame(game.gameRenderer.renderingLoop);
    else if (window.msRequestAnimationFrame)
        window.msRequestAnimationFrame(game.gameRenderer.renderingLoop);
    else if (window.webkitRequestAnimationFrame)
        window.webkitRequestAnimationFrame(game.gameRenderer.renderingLoop);
    else if (window.mozRequestAnimationFrame)
        window.mozRequestAnimationFrame(game.gameRenderer.renderingLoop);
    else if (window.oRequestAnimationFrame)
        window.oRequestAnimationFrame(game.gameRenderer.renderingLoop);
    else {
        game.gameRenderer.queueNewFrame = function () {};
        intervalRender = window.setInterval(game.gameRenderer.renderingLoop, 16.7);
    }
}
//Render loop
gamerenderer.prototype.renderingLoop = function(){
    if(this.useTicker){
        createjs.Ticker.setFPS(60);
        createjs.Ticker.addEventListener("tick",function(e){
            if(!e.paused){
                //game.gameMap.update();
                game.stage.update();
                if(game.gameRenderer.toggleFPS === true)
                    game.gameRenderer.FPS = createjs.Ticker.getMeasuredFPS();
            }
        });
    }else{
        if(!game.gameRenderer.stopRendering){
            //game.gameMap.update();
            game.stage.update();
        }
        game.gameRenderer.queueNewFrame();
    }
}

